import { legacy_createStore as createStore } from 'redux';
import { SelectionMode, StoreAction, StoreState } from '../types/store.ts';

const initialState: StoreState = {
  mode: SelectionMode.File,
  pen: [],
};

function reducer(state = initialState, action: StoreAction) {
  switch (action.type) {
    case 'CHANGE_SELECT_MODE':
      if (!action.payload || action.payload.length <= 0) {
        return {
          mode: SelectionMode.File,
          pen: [],
        };
      }
      if (action.payload.length == 1) {
        return {
          mode: SelectionMode.Pen,
          pen: action.payload,
        };
      }
      if (action.payload.length > 1) {
        return {
          mode: SelectionMode.Multiple,
          pen: action.payload,
        };
      }
      return {
        mode: SelectionMode.File,
        pen: [],
      };
    default:
      return state;
  }
}

const store = createStore(reducer);

export default store;
